package yawning.mew.cat;

import yawning.mew.ProcEffect.Trigger;
import yawning.mew.cat.CatModelSimState.CatTargetData;
import yawning.mew.character.CharacterModel.AttackResult;
import yawning.mew.sim.SimulationAction;
import yawning.mew.sim.SimulationTarget;

public class MaimAction implements SimulationAction {
	CatModelSimState mState;
	private SimulationTarget mTarget;
	private CatTargetData mTargetData;
	private String mTargetName;

	MaimAction(CatModelSimState state, CatTargetData targetData) {
		mState = state;
		mTarget = targetData.getSimulationTarget();
		mTargetData = targetData;
		mTargetName = mTarget.getTarget().getName();
	}

	public double perform() {
		int cps = mState.getComboPoints();
		int cost = mState.getEnergyCost(mState.mModel.maimCost);
		if ((mState.getEnergy() < cost && !mState.isOoCUp()) || mState.getComboPoints() == 0) return SimulationAction.ACTION_FAIL;

		// Note: I check the cooldown in CatModelSimState.

		int prevEnergy = mState.getEnergy();

		AttackResult result = (mTargetData.mCanParry) ? mState.mModel.getFrontalYellowResult() : mState.mModel.getYellowResult();

		if (result == AttackResult.MISS) {
			cost = (int) Math.round(cost * 0.2);
			mNrMaimMiss++;

			if (mState.mIsLogging) mState.mLog.log("Maim: %s MISS\n", mTargetName);
		} else if (result == AttackResult.DODGE) {
			cost = (int) Math.round(cost * 0.2);
			mNrMaimDodge++;

			if (mState.mIsLogging) mState.mLog.log("Maim: %s DODGE\n", mTargetName);
		} else if (result == AttackResult.PARRY) {
			cost = (int) Math.round(cost * 0.2);
			mNrMaimParry++;

			if (mState.mIsLogging) mState.mLog.log("Maim: %s PARRY\n", mTargetName);
		} else {
			double damage = mState.mModel.levelDep.getMaimBaseDmg() + mState.mModel.levelDep.getMaimCpDmg() * cps + mState.mModel.getWeaponDamage() * mState.mModel.levelDep.getMaimMult();

			if (result == AttackResult.CRIT) {
				mNrMaimCrit++;
				damage *= mState.mModel.multCrit;
			} else {
				mNrMaimHit++;
			}

			damage *= mState.getDirectDamageMult();
			mMaimDamage += Math.round(damage);

			if (mState.mIsLogging) mState.mLog.log("Maim: %s %s Damage: %d\n", mTargetName, result, Math.round(damage));

			if (result == AttackResult.CRIT) mState.mSimEngine.triggerProc(mTarget, Trigger.YELLOWCRIT); else mState.mSimEngine.triggerProc(mTarget, Trigger.YELLOW);

			mNrMaimCPs += cps;

			mState.mBerserkEv.onTier12_4pc_tigger(cps);
			mState.onFinisher();

			mTarget.getTargetStunEv().controlledStun(cps * mState.mModel.maimStunDuration);
		}

		if (mState.mOocEv.isUp()) mNrMaimOoC++;
		if (mState.mTigersFuryEv.isUp()) mNrMaimTF++;

		mMaimEnergy += mState.onOoCConsumer(cost);

		mState.logResourceChange(prevEnergy, cps);

		return 1.0f;
	}

	long mMaimDamage;
	int mNrMaimMiss;
	int mNrMaimDodge;
	int mNrMaimParry;
	int mNrMaimCrit;
	int mNrMaimHit;

	int mMaimEnergy;
	int mNrMaimCPs;
	int mNrMaimOoC;
	int mNrMaimTF;
}
